package plugins

import (
	"time"

	"github.com/quarkcloudio/quark-go/v2/pkg/app/tool/template/upload"
	"github.com/quarkcloudio/quark-go/v2/pkg/builder"
	"github.com/quarkcloudio/quark-go/v2/pkg/storage"
	"github.com/wsl/hi-go/config"
)

// 图片上传
type Image struct {
	upload.Template
}

// 初始化
func (p *Image) Init(ctx *builder.Context) interface{} {

	// 限制文件大小
	p.LimitImageHeight = 1024 * 1024 * 100

	// 限制文件类型
	p.LimitType = []string{
		"image/png",
		"image/gif",
		"image/jpeg",
		"video/mp4",
		"audio/mpeg",
		"video/mpeg",
		"application/x-xls",
		"application/x-ppt",
		"application/msword",
		"application/zip",
		"application/pdf",
		"application/vnd.ms-excel",
		"application/vnd.ms-powerpoint",
		"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
		"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
		"application/vnd.openxmlformats-officedocument.presentationml.presentation",
	}

	// 设置文件上传路径
	p.SavePath = config.APP.UploadFilePath + time.Now().Format("20060102") + "/"

	return p
}

// 上传前回调
func (p *Image) BeforeHandle(ctx *builder.Context, fileSystem *storage.FileSystem) (*storage.FileSystem, *storage.FileInfo, error) {
	return fileSystem, nil, nil
}

// 上传完成后回调
func (p *Image) AfterHandle(ctx *builder.Context, result *storage.FileInfo) error {

	return ctx.JSONOk("上传成功", map[string]interface{}{
		"url": result.Url,
	})
}
